package com.app.shop;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.app.shop.activitys.ShopCarActivity;
import com.app.shop.apadapter.MyApadapter;
import com.app.shop.fragments.DTFragment;

import com.app.shop.activitys.LoginActivity;
import com.app.shop.fragments.FenleiFragment;
import com.app.shop.fragments.HomeFragment;
import com.app.shop.fragments.MineFragment;
import com.app.library.base.BaseActivity;
import com.app.library.utils.UserManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {


    TextView txt1;

    TextView txt2;

    TextView txt3;

    TextView txt4;

    ViewPager mFragment;

    ImageView imgvCar;
    private    List<Fragment> fragmentList;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         txt1 = (TextView) findViewById(R.id.txt_1);
         txt2 = (TextView) findViewById(R.id.txt_2);
         txt3 = (TextView) findViewById(R.id.txt_3);
         txt4 = (TextView) findViewById(R.id.txt_4);
        mFragment = (ViewPager) findViewById(R.id.fragment_container);
        imgvCar = (ImageView) findViewById(R.id.imgv_car);
        txt1.setOnClickListener(this::onViewClicked);
        txt2.setOnClickListener(this::onViewClicked);
        txt3.setOnClickListener(this::onViewClicked);
        txt4.setOnClickListener(this::onViewClicked);
        imgvCar.setOnClickListener(this::onViewClicked);
        initData();
    }

    @Override
    public void onBackPressed() {
//        finish();
    }

    private void initData() {
        Log.i("","33333");
        fragmentList=new ArrayList<>();
        fragmentList.add(HomeFragment.getInstance());
        fragmentList.add(FenleiFragment.getInstance());
        fragmentList.add(DTFragment.getInstance());
        fragmentList.add(MineFragment.getInstance());
        initFragment();

    }

    private void initFragment() {
        mFragment.setAdapter(new MyApadapter(getSupportFragmentManager(),fragmentList));
        mFragment.setCurrentItem(0);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        fragmentList.clear();
    }







    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_1:

                mFragment.setCurrentItem(0);
                break;
            case R.id.txt_2:
                mFragment.setCurrentItem(1);
                break;
            case R.id.txt_3:
                mFragment.setCurrentItem(2);

                break;
            case R.id.txt_4:
                mFragment.setCurrentItem(3);

                break;

            case R.id.imgv_car:
                if (!UserManager.isLogin(this)) {
                    showActivity(this, LoginActivity.class);
                } else {
                    showActivity(this, ShopCarActivity.class);
                }
                break;
        }
    }



}
